import 'package:bilibili/util/color.dart';
import 'package:flutter/material.dart';

class LoginInput extends StatefulWidget {
  final String? title;
  final String? hint;
  final ValueChanged<String>? onChanged;
  final ValueChanged<bool>? foucusChanged;
  final bool? linStretch;
  final bool? obscureText;
  final TextInputType? keyboardType;

  LoginInput(this.title, this.hint,
      {Key? key,
      this.onChanged,
      this.foucusChanged,
      this.linStretch = false,
      this.obscureText = false,
      this.keyboardType})
      : super(key: key);

  // ignore: empty_constructor_bodies
  @override
  State<StatefulWidget> createState() {
    return _LoginInputState();
  }
}

class _LoginInputState extends State<LoginInput> {
  final _foucusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    _foucusNode.addListener(() {
      if (widget.foucusChanged != null) {
        widget.foucusChanged!(_foucusNode.hasFocus);
      }
    });
  }

  @override
  void dispose() {
    _foucusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Container(
              padding: EdgeInsets.only(left: 15),
              width: 100,
              child: Text(
                widget.title ?? "",
                style: TextStyle(fontSize: 16),
              ),
            ),
            _input()
          ],
        ),
        Padding(
          padding: EdgeInsets.only(left: (widget.linStretch ?? true) ? 15 : 0),
          child: Divider(
            height: 1,
            thickness: 0.5,
          ),
        )
      ],
    );
  }

  Widget _input() {
    return Expanded(
        child: TextField(
      focusNode: _foucusNode,
      onChanged: widget.onChanged,
      obscureText: widget.obscureText ?? false,
      keyboardType: widget.keyboardType,
      autofocus: !(widget.obscureText ?? false),
      cursorColor: primary.shade50,
      style: TextStyle(fontSize: 16, fontWeight: FontWeight.w300),
      decoration: InputDecoration(
          contentPadding: EdgeInsets.only(left: 20, right: 20),
          border: InputBorder.none,
          hintText: widget.hint,
          hintStyle: TextStyle(fontSize: 15, color: Colors.grey)),
    ));
  }
}
